package com.iori.sort;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class SortReducer extends Reducer<IntWritable, NullWritable, Text, NullWritable> {
    private static int countnum = 0;
    private static int temNum = 0;

    @Override
    protected void reduce(IntWritable key, Iterable<NullWritable> values, Context context) throws IOException, InterruptedException {
        Text kout = new Text();
        IntWritable valueout = new IntWritable();
        for (NullWritable value : values) { //遍历value,复制到list。
            if (key.get() > temNum) {
                countnum++;//全局排序变量
                temNum = key.get();//记录当前临时值
            }
            String kk = countnum + "\t" + key.toString();
            kout.set(kk);
            context.write(kout, NullWritable.get());
        }

    }
}

